﻿using System.Text;
using HttpService.Collect;
using RpcHelper;
using RpcHelper.Config;

namespace HttpService.Config
{
    internal class DefHttpConfig
    {
        private void Local_RefreshEvent(IConfigCollect config, string name)
        {
            if (name == "http:log" || name.StartsWith("http:log:"))
            {
                this.Log = config.GetValue<LogConfig>("http:log", new LogConfig());
                this.Log.Init();
            }
            else if (name == "http:certHashVal")
            {
                this.CertHashVal = config.GetValue(name);
            }
        }
        public void init()
        {
            LocalConfig.Local.RefreshEvent += this.Local_RefreshEvent;
            this.File.Init();
            this.Gzip.Init();
            this.Up.Init();
            this.Log.Init();
        }
        public LogConfig Log { get; set; }
        public HttpFileConfig File { get; set; }

        public GzipConfig Gzip { get; set; }

        public UpConfig Up { get; set; }

        /// <summary>
        /// 证书的Hash值
        /// </summary>
        public string CertHashVal { get; set; }


        private string _ResponseEncoding;
        /// <summary>
        /// 响应编码
        /// </summary>
        public string ResponseEncoding
        {
            get => this._ResponseEncoding;
            set
            {
                if (value == this._ResponseEncoding)
                {
                    return;
                }
                else if (value == null)
                {
                    this._ResponseEncoding = "utf-8";
                    this.Encoding = Encoding.UTF8;
                }
                else
                {
                    this._ResponseEncoding = value;
                    this.Encoding = Encoding.GetEncoding(value);
                }
            }
        }

        internal Encoding Encoding
        {
            get;
            private set;
        }


    }
}
